package com.excellent.item.controller;

import com.excellent.item.pojo.SpecGroup;
import com.excellent.item.pojo.SpecParam;
import com.excellent.item.service.SpecificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Xxacker
 */
@Slf4j
@RestController
@RequestMapping("spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 【商品详情页 -> 】
     * 根据分类id查询分组 Group
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupsByCid(@PathVariable("cid")Long cid){
        List<SpecGroup> groups = this.specificationService.queryGroupsByCid(cid);
        if (CollectionUtils.isEmpty(groups)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groups);
    }

    /**
     * 新增规格参数组 Group
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveSpecGroup(SpecGroup specGroup){
        this.specificationService.saveSpecGroup(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数组 Group
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroup(
            @RequestParam(value = "id", required = false)Long id,
            @RequestParam(value = "cid", required = false)Long cid,
            @RequestParam(value = "name")String name,
            @RequestParam(value = "params", required = false)String params){
        SpecGroup specGroup =  new SpecGroup();
            specGroup.setId(id);
            specGroup.setCid(cid);
            specGroup.setName(name);
            specGroup.setParams(stringToList(params));
        this.specificationService.updateSpecGroup(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除规格参数组 Group
     * @param id
     */
    @DeleteMapping("group/delete/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id){
        log.info(id.toString());
        this.specificationService.deleteSpecGroup(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**【规格参数管理功能 -> 根据分组ID 查询规格参数】
     * 【新增商品 -> 根据分类，查询规格参数
     *              根据是否是通用属性，查询规格参数
     *              根据是否是查询字段，查询规格参数
     * 】
     * 根据条件查询规格参数 Params
     * @param gid  规格参数组 group_ID
     * @param cid  分类Id            【新增商品功能】
     * @param generic    通用规格参数
     * @param searching  是否用作过滤
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid", required = false)Long gid,
            @RequestParam(value = "cid", required = false)Long cid,
            @RequestParam(value = "generic", required = false)Boolean generic,
            @RequestParam(value = "searching", required = false)Boolean searching
    ){
        List<SpecParam>  params = this.specificationService.queryParams(gid, cid, generic, searching);
//        if (CollectionUtils.isEmpty(params)){
//            return ResponseEntity.notFound().build();
//        }
        return ResponseEntity.ok(params);
    }

    /**
     * 新增规格参数 Params
     */
    @PostMapping("param")
    public ResponseEntity<Void> saveParams(SpecParam specParam){
        this.specificationService.saveSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数 Params
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateParams(SpecParam specParam){
        this.specificationService.updateSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 删除规格参数 Params
     */
    @PostMapping("param/delete/{id}")
    public ResponseEntity<Void> deleteParams(@PathVariable("id") Long id){
        this.specificationService.deleteSpecParam(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询规格参数组，及组内参数
     * */
    @GetMapping("{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecsByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> list = this.specificationService.querySpecsByCid(cid);
        if(list == null || list.size() == 0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        log.info("查询规格参数组的参数{}",list.toString());
        return ResponseEntity.ok(list);
    }

    /**
     * String 转 List
     * @param strs
     * @return
     */
    private List<SpecParam> stringToList(String strs){
        List<String> specParamStr = Arrays.asList(strs.split(","));
        List<SpecParam> specParamList = new ArrayList<>();
        specParamStr.forEach(log::info);
        return specParamList;
    }
}
